# CMakeLists.txt
# -------------------------------------------------------
# An initial version of the cmake build for rssguard.
# WORK IN PROGRESS. USE qmake STILL FOR NOW, PLEASE!
# Currently tested on unix only.
# -------------------------------------------------------
# UNIX - usage (for OSX see below)
# 1. cd rssguard/root/dir
# 2. mkdir build
# 3. cd build
# 4. cmake ../rssguard-git -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=debug (or release, by default)
# 5. make
# 6. make install
# -------------------------------------------------------
# WINDOWS - usage
# You have to have MinGW installed  (it works with MinGW). If you use different compiler, then "make" steps will differ.
# You have to have CMake installed.
# I recommend using cmake-gui to generate and configure MakeFiles like this.
# 1. Run cmake-gui and select folder which contains top-level CMakelists.txt file into "Where the is source code" box. This can be also done in command line via simpler cmake.exe.
# 2. Select folder which you want to contain compiled binaries into "Where to build the binaries" box.
# 3. Hit Configure button (setup -DWINDRES path variable to windres.exe) and select MinGW Makefiles as generator. Choose "Specify native compilers".
# 4. Now input path to C++ compiler. For example "C:/QtSDK/mingw/bin/g++.exe", then hit "Finish".
# 5. Project will configure now, hit "Configure" button again. Then hit "Generate" button and close cmake-gui.
# 6. Run your command line as ADMINISTRATOR and navigate to your "Where to build the binaries" folder.
# 7. Run "mingw32-make.exe" command. Note that -j3 parameter may speed-up building. Your files gets compiled now.
# 8. Run "mingw32-make.exe install" command. Your files gets copied into directory specified by CMAKE_INSTALL_PREFIX variable.
# 9. You are done.
# 10. Run "make install" to install application to CMAKE_INSTALL_PREFIX (which is se to C:\Program Files\rssguard\ by default - can be changed in step 3).
# -------------------------------------------------------
# MAC OS X - usage
# 1. cd rssguard/root/dir
# 2. mkdir build
# 3. cd build
# 4. cmake ../rssguard-git -DCMAKE_INSTALL_PREFIX=../release -DCMAKE_BUILD_TYPE=debug (or release, by default)
# 5. make
# 6. make install
#    Now is the RSSGuard.app bundle created. But it can run only in current machine.
#    If you need "redistributable" binary call:
# 7. make bundle
#    It will copy all dependecies (libs/resources) into bundle tree
# -------------------------------------------------------

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)

project(rssguard)

message(STATUS "[rss-guard] ENTERING cmake build process.")

set(RSSGUARD_VERSION 1.1.2)

if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
  find_package(Git)
  if(GIT_FOUND)
    execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    OUTPUT_VARIABLE _git_revision)
    string(REGEX REPLACE "\n" "" _git_revision "${_git_revision}")
    set(RSSGUARD_REVISION ${_git_revision})
  else(GIT_FOUND)
    set(RSSGUARD_REVISION NO-REVISION)
  endif(GIT_FOUND)
else(EXISTS "${PROJECT_SOURCE_DIR}/.git")
  set(RSSGUARD_REVISION STABLE)
endif(EXISTS "${PROJECT_SOURCE_DIR}/.git")

configure_file (
  ${PROJECT_SOURCE_DIR}/src/definitions.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/src/definitions.h
  )

add_definitions(-DWINDRES="${WINDRES}")

# define some useful DEBUG for, ehrm, debug build
if (CMAKE_BUILD_TYPE STREQUAL "debug" OR CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG")
    message(STATUS "[rss-guard] A debug build. -DDEBUG is defined")
    add_definitions(-DDEBUG)
else ()
    message(STATUS "[rss-guard] A release build (non-debug). Debugging outputs are silently ignored.")
    add_definitions(-DQT_NO_DEBUG_OUTPUT)
endif ()

# UNICODE things
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNICODE")
if (WIN32)
	# UNICODE support with Visual C++ and MinGW
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_UNICODE")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_UNICODE")
endif (WIN32)

# Find required libraries
find_package(Qt4 REQUIRED QtCore QtGui QtNetwork QtWebkit QtXml)
include(${QT_USE_FILE})
if (QT_QTDBUS_FOUND)
    message(STATUS "[rss-guard] Qt4 DBUS module found. It's good.")
    add_definitions(-DHAVE_DBUS)
else (QT_QTDBUS_FOUND)
    message(STATUS "[rss-guard] Qt4 DBUS is not found. Disabling it.")
endif (QT_QTDBUS_FOUND)


set(EXE_NAME "rssguard")
if (APPLE)
    set(EXE_NAME "RSSGuard")
endif (APPLE)


# source code and compilation
set (RSSGUARD_SOURCES 
    src/main.cpp
    src/formmain.cpp
    src/rssitem.cpp
    src/formabout.cpp
    src/rssfeed.cpp
    src/rssreader.cpp
    src/formeditfeed.cpp
    src/formsettings.cpp
    src/rssthread.cpp
    src/lineedit.cpp
    src/updateworker.cpp
    src/treewidget.cpp
	src/appfactory.cpp
    src/uifactory.cpp
    src/rssfactory.cpp
    src/formeditcategory.cpp
    src/singleapplication.cpp
)
if (APPLE)
    SET (RSSGUARD_SOURCES ${RSSGUARD_SOURCES} resources/macosx/rssguard.icns)
endif (APPLE)

set (RSSGUARD_HEADERS
    ${CMAKE_CURRENT_BINARY_DIR}/src/definitions.h
    src/formmain.h
    src/rssitem.h
    src/formabout.h
    src/rssfeed.h
    src/rssreader.h
    src/formeditfeed.h
    src/formsettings.h
    src/rssthread.h
    src/lineedit.h
    src/treewidget.h
    src/tabwidget.h
    src/updateworker.h
    src/uifactory.h
    src/appfactory.h
    src/timer.h
    src/systemtrayicon.h
    src/rssfactory.h
    src/formeditcategory.h
    src/singleapplication.h
)
if (QT_QTDBUS_FOUND)
    set ( RSSGUARD_HEADERS ${RSSGUARD_HEADERS} src/dbusadaptor.h)
endif (QT_QTDBUS_FOUND)

set (RSSGUARD_FORMS
    ui/formmain.ui
    ui/formabout.ui
    ui/formeditfeed.ui
    ui/formsettings.ui
    ui/formeditcategory.ui
)

set (RSSGUARD_RESOURCES
    RSSGuard.qrc
)

if (MINGW)
    message(STATUS "[rss-guard] MinGW compilation is selected. Icon file binary will be builded.")
    message(STATUS "[rss-guard] Used windres tool is: "${WINDRES})
    add_custom_command(	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/RSSGuard.o
			COMMAND ${WINDRES}  -I${CMAKE_CURRENT_SOURCE_DIR}
					    -i${CMAKE_CURRENT_SOURCE_DIR}/resources/RSSGuard_win.rc
					    -o ${CMAKE_CURRENT_BINARY_DIR}/RSSGuard.o)
    set(RSSGUARD_SOURCES ${RSSGUARD_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/RSSGuard.o)
else (MINGW)
    set(RSSGUARD_SOURCES ${RSSGUARD_SOURCES}  ${CMAKE_CURRENT_SOURCE_DIR}/resources/RSSGuard_win.rc)
endif (MINGW)


#elseif (EXISTS $ENV{PATH}/windres.exe)
#    message(STATUS "[rss-guard] Another compilation is selected. Icon file binary will be builded.")
#    message(STATUS "[rss-guard] Used windres tool is: "${WINDRES})
#    add_custom_command(	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/RSSGuard.o
#			COMMAND ${WINDRES}  -i ${CMAKE_CURRENT_SOURCE_DIR}
#					    -r ${CMAKE_CURRENT_SOURCE_DIR}/resources/RSSGuard_os2.rc
#					    ${CMAKE_CURRENT_BINARY_DIR}/RSSGuard.o)
#    set(RSSGUARD_SOURCES ${RSSGUARD_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/RSSGuard.o)

set (RSSGUARD_TRANSLATIONS
    localization/rssguard_cs.ts
    localization/rssguard_da.ts
    localization/rssguard_es.ts
    localization/rssguard_fr.ts
    localization/rssguard_ru.ts
)

QT4_WRAP_UI( RSSGUARD_UI ${RSSGUARD_FORMS} )
QT4_WRAP_CPP( RSSGUARD_MOC ${RSSGUARD_HEADERS} )
QT4_ADD_RESOURCES( RSSGUARD_RCC ${RSSGUARD_RESOURCES} )
QT4_ADD_TRANSLATION( RSSGUARD_QM ${RSSGUARD_TRANSLATIONS} )

include_directories (
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}/src
)

if (QT_QTDBUS_FOUND)
    include_directories(${QT_QTDBUS_INCLUDE_DIR})
endif (QT_QTDBUS_FOUND)

add_executable( ${EXE_NAME} WIN32 MACOSX_BUNDLE
    ${RSSGUARD_SOURCES}
    ${RSSGUARD_UI}
    ${RSSGUARD_MOC}
    ${RSSGUARD_RCC}
    ${RSSGUARD_QM}
)

target_link_libraries( ${EXE_NAME}
    ${QT_QTCORE_LIBRARY}
    ${QT_QTGUI_LIBRARY}
    ${QT_QTNETWORK_LIBRARY}
    ${QT_QTXML_LIBRARY}
    ${QT_QTMAIN_LIBRARY}
    ${QT_QTWEBKIT_LIBRARY}
)
if (QT_QTDBUS_FOUND)
    target_link_libraries( ${EXE_NAME} ${QT_QTDBUS_LIBRARY})
endif (QT_QTDBUS_FOUND)

# installation
# NOTE that you have to keep destinations of RSSGUARD_QM synced with defines in definitions.h.in
if (APPLE)
    # mac's bundle install
    SET_TARGET_PROPERTIES(${EXE_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/resources/macosx/Info.plist.in")
    SET(MACOSX_BUNDLE_ICON_FILE rssguard.icns)
    SET(MACOSX_BUNDLE_INFO_STRING "${EXE_NAME} ${RSSGUARD_VERSION}")
    SET(MACOSX_BUNDLE_GUI_IDENTIFIER "com.googlecode.rss-guard")
    SET(MACOSX_BUNDLE_LONG_VERSION_STRING "${RSSGUARD_VERSION}")
    SET(MACOSX_BUNDLE_BUNDLE_NAME "${EXE_NAME}")
    SET(MACOSX_BUNDLE_SHORT_VERSION_STRING "${RSSGUARD_VERSION}")
    SET(MACOSX_BUNDLE_BUNDLE_VERSION "${RSSGUARD_VERSION}")
    SET(MACOSX_BUNDLE_COPYRIGHT "(c) Martin Rotter")
    SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/resources/macosx/rssguard.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
    INSTALL(TARGETS ${EXE_NAME} BUNDLE DESTINATION ${CMAKE_INSTALL_PREFIX})
    install(FILES ${RSSGUARD_QM} DESTINATION ${CMAKE_INSTALL_PREFIX}/${EXE_NAME}.app/Contents/Resources/l10n)
    # create a "transportable" bundle - all libs into the bundle: "make bundle" after make install
    configure_file(resources/macosx/bundle.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/bundle.cmake @ONLY)
    add_custom_target(bundle ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/bundle.cmake)
elseif (WIN32)
    message(STATUS "[rss-guard] Installing on Windows.")

    install(TARGETS ${EXE_NAME} RUNTIME DESTINATION ./)
    install(FILES ${RSSGUARD_QM} DESTINATION ./l10n)
elseif (OS2)
    ## here we have to fix OS2 make install
    message(STATUS "[rss-guard] Installing on OS/2.")

    install(TARGETS ${EXE_NAME} RUNTIME DESTINATION ./)
    install(FILES ${RSSGUARD_QM} DESTINATION ./l10n)
elseif (UNIX)
    message(STATUS "[rss-guard] Installing on Linux.")

    install(TARGETS ${EXE_NAME} RUNTIME DESTINATION bin)
    install(FILES resources/desktops/rssguard.desktop DESTINATION share/applications)
    install(FILES resources/graphics/rssguard.png DESTINATION share/pixmaps)
    install(FILES ${RSSGUARD_QM} DESTINATION share/rssguard/l10n)
endif ()



# make dist custom target
SET(CPACK_PACKAGE_NAME "rssguard")
SET(CPACK_PACKAGE_VERSION ${RSSGUARD_VERSION})
SET(CPACK_SOURCE_GENERATOR "TGZ")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
SET(CPACK_IGNORE_FILES "/CVS/;/\\\\.svn/;/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.tar.gz$;/CMakeFiles/;CMakeCache.txt;\\\\.qm$;/build/;\\\\.diff$;.DS_Store'")
SET(CPACK_SOURCE_IGNORE_FILES ${CPACK_IGNORE_FILES})
INCLUDE(CPack)
# simulate autotools' "make dist"
add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)


# make lupdate
# it generates new translation files
add_custom_target(lupdate
	${QT_QMAKE_EXECUTABLE} -project -o ${CMAKE_CURRENT_SOURCE_DIR}/rssguard.pro
	COMMAND ${QT_LUPDATE_EXECUTABLE} -noobsolete ${CMAKE_CURRENT_SOURCE_DIR}/rssguard.pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
add_custom_target(lrelease
	${QT_QMAKE_EXECUTABLE} -project -o ${CMAKE_CURRENT_SOURCE_DIR}/rssguard.pro
	COMMAND ${QT_LRELEASE_EXECUTABLE} -compress ${CMAKE_CURRENT_SOURCE_DIR}/rssguard.pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)



